#!/bin/bash
function uninit()
{
    echo -ne "\a"
    rm -rf $w
    echo
}
w=$(mktemp -d)
if [ -n "$1" ]  ; then
    I=`cat $1`
    test -n "$1" -a -f "$1"  || { echo "give at least one parmeter - film name" ; exit 1 ; }
    S="$2"
    O=${3:-$1.mp4}
    shift 3
else
    I=`zenity --file-selection --title="plik z filmem"`
    if [ "-z" "$I" ] ; then
	exit 1
    fi
    S=`zenity --file-selection --title="napis"`
    O=`zenity --entry --title="plik wyjsciowy"`.mp4
    if [ "-z" "$O" ] ; then
	exit 1
    fi
fi
echo ${PROFILE:=k800}
trap uninit EXIT
mkfifo $w/tran.avi

mencoder ${S:+-sub "$S"} -profile p320raw -oac copy -ovc raw -o $w/tran.avi $I  &
sleep 1
cat $w/tran.avi  | ffmpeg -y -i - -f mp4 -vcodec xvid -maxrate 320 -b 300 -qmin 3 -qmax 5 -bufsize 4096 -g 300 -acodec aac -ab 64  "$O" &> /dev/null 
wait
